package com.sicheng.lc.周赛.分类.前缀和or差分数组;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/16 16:23
 */
public class 最小平均差 {
    //https://leetcode.cn/problems/minimum-average-difference/
    public int minimumAverageDifference(int[] nums) {
        if (nums.length == 1)
            return 0;
        long sum = 0;
        for (int num : nums) {
            sum += num;
        }

        long min = Integer.MAX_VALUE;
        long pre = 0;
        int index = nums.length;
        long abs;
        for (int i = 0; i < nums.length; i++) {
            pre += nums[i];
            abs = Math.abs(avg(pre, i + 1) - avg(sum - pre, nums.length - i - 1));
            if (abs < min) {
                min = abs;
                index = i;
            }
        }
        return index;
    }

    private long avg(long l, int n) {
        return n == 0 ? 0 : l / n;
    }

    public static void main(String[] args) {
        最小平均差 solution = new 最小平均差();
        int[] numbers = {2, 5, 3, 9, 5, 3};
        System.out.println(solution.minimumAverageDifference(numbers));
    }
}
